using MultiAgentsClient.Shared;
using MultiAgentsServer.Scripts.Shared;
using MultiAgentsShared;

namespace MultiAgentsClient.ChatDataStructures;

public class UserDataModel : SerializableObjectBase
{
    public int Id;
    public string Name = NameConsts.DefaultUserName;
    public List<ChatProject> chatProjects = [];
    public List<ChatProject> chatProjectsArchived = [];
    public bool allowToolCall = false;

    public int selectedPrjId = 0;
    public int selectedChatRoomId = 0;

    public List<string> chatAccessibleLlmModels = [];
    public List<string> workflowAccessibleLlmModels = [];
    public List<AgentTemplate> customAgents = [];

    public UserDataModel() { }

    public UserDataModel(int id, string name)
    {
        Id = id;
        Name = name;
    }

    public UserDataModel DeepCopy(UserDataModel userDataModel)
    {
        Id = userDataModel.Id;
        Name = userDataModel.Name;
        chatProjects = userDataModel.chatProjects;

        selectedPrjId = userDataModel.selectedPrjId;
        selectedChatRoomId = userDataModel.selectedChatRoomId;

        chatAccessibleLlmModels = userDataModel.chatAccessibleLlmModels;
        workflowAccessibleLlmModels = userDataModel.workflowAccessibleLlmModels;
        customAgents = userDataModel.customAgents;
        allowToolCall = userDataModel.allowToolCall;

        return this;
    }

#if FRONTEND //TODO: in backend it would be handled in collection service
    public ChatRoom? GetChatRoom(int chatRoomId)
    {
        foreach (ChatProject chatProject in chatProjects)
        {
            ChatRoom? chatRoom = chatProject.ChatRoomList.FirstOrDefault(cr => cr.Id == chatRoomId);
            if (chatRoom != null)
            {
                return chatRoom;
            }
        }
        return null;
    }

    public ChatProject? GetChatProject(int chatProjectId)
    {
        return chatProjects.FirstOrDefault(cp => cp.Id == chatProjectId);
    }

    public List<LlmMessageSimp>? GetCurrentRoomHistory()
    {
        ChatRoom? chatRoom = GetChatRoom(selectedChatRoomId);
        return chatRoom?.defaultAgent.ChatListHistoryLlmSimp();
    }
#endif

    public ChatProject CreateProject(int projectId, string projectName, string? projectSetting)
    {
        ChatProject newProject = new(projectId, projectName, projectSetting);
        chatProjects.Add(newProject);

        return newProject;
    }

    public bool DeleteProject(int projectId)
    {
        ChatProject? projectToRemove = chatProjects.FirstOrDefault(p => p.Id == projectId);

        if (projectToRemove != null)
        {
            chatProjects.Remove(projectToRemove);
            return true;
        }

        return false;
    }

    public bool UpdateMessageContent(int messageId, string content)
    {
        ChatMessageData? message = chatProjects
            .SelectMany(project => project.ChatRoomList)
            .SelectMany(room => room.defaultAgent.ChatListContainer.ChatList)
            .FirstOrDefault(msg => msg.Id == messageId) ?? throw Debug.ShouldNotHappen();

        message.content = content;

        return true;
    }
}
